/*
 COPYRIGHT 2009 ESRI
 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.
 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA
 email: contracts@esri.com
 */
//>>built
define("esri/virtualearth/VETiledLayer", ["dijit", "dojo", "dojox", "dojo/require!esri/utils,esri/layers/tiled,esri/geometry,dojo/string"], function(_1, _2, _3){
    _2.provide("esri.virtualearth.VETiledLayer");
    _2.require("esri.utils");
    _2.require("esri.layers.tiled");
    _2.require("esri.geometry");
    _2.require("dojo.string");
    _2.declare("esri.virtualearth.VETiledLayer", esri.layers.TiledMapServiceLayer, {
        constructor: function(_4){
            try {
                _4 = _2.mixin({
                    bingMapsKey: null,
                    culture: "en-US"
                }, _4 ||
                {});
                var _5 = window.location.protocol;
                if (_5 === "file:") {
                    _5 = "http:";
                }
                this.url = _5 + "//dev.virtualearth.net/REST/v1";
                this._url = esri.urlToObject(this.url);
                this.spatialReference = new esri.SpatialReference({
                    wkid: 102100
                });
                this.tileInfo = new esri.layers.TileInfo({
                    rows: 256,
                    cols: 256,
                    dpi: 96,
                    origin: {
                        x: -20037508.342787,
                        y: 20037508.342787
                    },
                    spatialReference: {
                        wkid: 102100
                    },
                    lods: [{
                        level: 1,
                        resolution: 78271.5169639999,
                        scale: 295828763.795777
                    }, {
                        level: 2,
                        resolution: 39135.7584820001,
                        scale: 147914381.897889
                    }, {
                        level: 3,
                        resolution: 19567.8792409999,
                        scale: 73957190.948944
                    }, {
                        level: 4,
                        resolution: 9783.93962049996,
                        scale: 36978595.474472
                    }, {
                        level: 5,
                        resolution: 4891.96981024998,
                        scale: 18489297.737236
                    }, {
                        level: 6,
                        resolution: 2445.98490512499,
                        scale: 9244648.868618
                    }, {
                        level: 7,
                        resolution: 1222.99245256249,
                        scale: 4622324.434309
                    }, {
                        level: 8,
                        resolution: 611.49622628138,
                        scale: 2311162.217155
                    }, {
                        level: 9,
                        resolution: 305.748113140558,
                        scale: 1155581.108577
                    }, {
                        level: 10,
                        resolution: 152.874056570411,
                        scale: 577790.554289
                    }, {
                        level: 11,
                        resolution: 76.4370282850732,
                        scale: 288895.277144
                    }, {
                        level: 12,
                        resolution: 38.2185141425366,
                        scale: 144447.638572
                    }, {
                        level: 13,
                        resolution: 19.1092570712683,
                        scale: 72223.819286
                    }, {
                        level: 14,
                        resolution: 9.55462853563415,
                        scale: 36111.909643
                    }, {
                        level: 15,
                        resolution: 4.77731426794937,
                        scale: 18055.954822
                    }, {
                        level: 16,
                        resolution: 2.38865713397468,
                        scale: 9027.977411
                    }, {
                        level: 17,
                        resolution: 1.19432856685505,
                        scale: 4513.988705
                    }, {
                        level: 18,
                        resolution: 0.597164283559817,
                        scale: 2256.994353
                    }, {
                        level: 19,
                        resolution: 0.298582141647617,
                        scale: 1128.497176
                    }]
                });
                this.initialExtent = (this.fullExtent = new esri.geometry.Extent(-20037508.342787, -20037508.34278, 20037508.34278, 20037508.342787, new esri.SpatialReference({
                    wkid: 102100
                })));
                _2.mixin(this, _4);
                this.hasAttributionData = this.showAttribution;
                this._initLayer = _2.hitch(this, this._initLayer);
                this._errorHandler = _2.hitch(this, this._errorHandler);
                this._getTileInfo = _2.hitch(this, this._getTileInfo);
                if (this.bingMapsKey) {
                    this._getTileInfo();
                }
                else {
                    throw new Error(esri.bundle.virtualearth.vetiledlayer.bingMapsKeyNotSpecified);
                }
            } 
            catch (e) {
                this.onError(e);
                throw e;
            }
        },
        _unsetMap: function(_6, _7){
            this.inherited("_unsetMap", arguments);
        },
        _getTileInfo: function(){
            if (!this.mapStyle) {
                return;
            }
            var _8 = this._url.path + "/Imagery/Metadata/" + this.mapStyle;
            if (this.bingMapsKey) {
                var _9 = this.resourceInfo;
                if (!this.loaded && _9) {
                    this._initLayer(_9);
                }
                else {
                    esri.request({
                        url: _8,
                        content: _2.mixin({}, {
                            key: this.bingMapsKey,
                            ss: true,
                            c: this.culture,
                            include: this.hasAttributionData ? "imageryProviders" : null
                        }),
                        callbackParamName: "jsonp",
                        load: this._initLayer,
                        error: this._errorHandler
                    });
                }
            }
        },
        _initLayer: function(_a, io){
            try {
                this.resourceInfo = _2.toJson(_a);
                var _b = _a.resourceSets[0].resources[0];
                var _c = _b.imageUrl.replace("{", "${");
                this.tileServers = _2.map(_b.imageUrlSubdomains, function(_d){
                    var _e = window.location.protocol;
                    if (_e === "file:") {
                        _e = "http:";
                    }
                    return _2.string.substitute(_c, {
                        subdomain: _d
                    }).replace("http:", _e);
                });
                this._tsLength = this.tileServers.length;
                if (!this.loaded) {
                    this.copyright = this.copyright || "&copy; 2012 Microsoft Corporation and its data suppliers";
                    this.loaded = true;
                    this.onLoad(this);
                    var _f = this.loadCallback;
                    if (_f) {
                        delete this.loadCallback;
                        _f(this);
                    }
                }
                else {
                    this.refresh();
                    this.onMapStyleChange();
                }
            } 
            catch (e) {
                this.onError(e);
            }
        },
        getAttributionData: function(){
            var dfd = new _2.Deferred(), _10 = _2.fromJson(this.resourceInfo), _11;
            if (this.hasAttributionData && _10) {
                _11 = _2.getObject("resourceSets.0.resources.0.imageryProviders", false, _10);
            }
            if (_11) {
                dfd.callback({
                    contributors: _11
                });
            }
            else {
                var err = new Error("Layer does not have attribution data");
                err.log = _2.config.isDebug;
                dfd.errback(err);
            }
            return dfd;
        },
        getTileUrl: function(_12, row, col){
            var _13 = this.tileServers[row % this._tsLength], _14 = _13.replace(/\{/g, "${");
            return _2.string.substitute(_14, {
                quadkey: this._getQuadKey(_12, row, col),
                culture: this.culture,
                token: this.bingMapsKey
            });
        },
        _getQuadKey: function(_15, row, col){
            var _16 = "", _17, _18, i;
            for (i = _15; i > 0; i--) {
                _17 = "0";
                _18 = 1 << (i - 1);
                if ((col & _18) != 0) {
                    _17++;
                }
                if ((row & _18) != 0) {
                    _17++;
                    _17++;
                }
                _16 = _16 + _17;
            }
            return _16;
        },
        setMapStyle: function(_19){
            this.mapStyle = _19;
            this._getTileInfo();
        },
        setCulture: function(_1a){
            this.culture = _1a;
            this._getTileInfo();
        },
        setBingMapsKey: function(_1b){
            this.bingMapsKey = _1b;
        },
        onMapStyleChange: function(){
        }
    });
    _2.mixin(esri.virtualearth.VETiledLayer, {
        MAP_STYLE_AERIAL: "aerial",
        MAP_STYLE_AERIAL_WITH_LABELS: "aerialWithLabels",
        MAP_STYLE_ROAD: "road"
    });
});
